package com.lzx.iteam;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.adapter.AttendanceDevicesAdapter;
import com.lzx.iteam.bean.DeviceMsgData;
import com.lzx.iteam.net.*;
import com.lzx.iteam.widget.MyListView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Megan on 16/4/14.
 */
public class AttendanceDevicesActivity extends Activity implements View.OnClickListener {

    private LinearLayout mLlBack;
    private TextView mTvDeviceName, mTvDeviceId, mTvDeviceStatus;
    private TextView mTvCurDeviceName, mTvCurDeviceId, mTvCurDeviceBind;
    private TextView mTvHistoryText;
    private MyListView mLvHistoryDeviceList;
    private final int GET_ATTENDANCE_GET_MY_DEVICE = 1000;
    private final int GET_ATTENDANCE_SET_MY_DEVICE = 1001;
    private String mDeviceId;
    private String mGroupId;
    private String mDeviceName;
    private int isOpen;//1.管理员已打开更换设备，0.未
    private ArrayList<DeviceMsgData> deviceMsgDatas = new ArrayList<DeviceMsgData>();
    private DeviceMsgData curDevice = new DeviceMsgData();
    private AttendanceDevicesAdapter mAdapter;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GET_ATTENDANCE_GET_MY_DEVICE:
                    if (msg.arg1 == 0) {
                        isOpen = msg.arg2;
                        deviceMsgDatas.clear();
                        ArrayList<DeviceMsgData> devices = (ArrayList<DeviceMsgData>) msg.obj;
                        for (DeviceMsgData deviceMsgData : devices) {
                            if ("1".equals(deviceMsgData.getGroupDefault())) {
                                curDevice = deviceMsgData;
                                mTvDeviceStatus.setText("已绑定");
                                mTvDeviceName.setText(deviceMsgData.getDeviceName());
                                mTvDeviceId.setText("设备号:" + deviceMsgData.getDeviceId());
                            } else {
                                deviceMsgDatas.add(deviceMsgData);
                            }
                        }
                        if (isOpen == 1) {
                            mTvCurDeviceBind.setTextColor(getResources().getColor(R.color.schedule_content));
                        } else {
                            mTvCurDeviceBind.setTextColor(getResources().getColor(R.color.attendance_bind_unclick));
                        }

                        if(deviceMsgDatas.size()>0){
                            mAdapter = new AttendanceDevicesAdapter(AttendanceDevicesActivity.this, isOpen);
                            mLvHistoryDeviceList.setAdapter(mAdapter);
                            mAdapter.bindData(deviceMsgDatas);
                        }else{
                            mTvHistoryText.setVisibility(View.GONE);
                        }
                    } else {
                        Toast.makeText(AttendanceDevicesActivity.this, (String) msg.obj, Toast.LENGTH_LONG).show();
                    }
                    break;
                case GET_ATTENDANCE_SET_MY_DEVICE:
                    if (msg.arg1 == 0) {
                        getMyDeviceMsg();
                    } else {
                        Toast.makeText(AttendanceDevicesActivity.this, (String) msg.obj, Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attendance_devices_layout);
        initViews();
        getIntentData();
        getCurDeviceMsg();
        getMyDeviceMsg();
    }

    private void initViews() {
        mLlBack = (LinearLayout) findViewById(R.id.ll_attendance_devices_back);
        mTvDeviceName = (TextView) findViewById(R.id.tv_attendance_devices_name);
        mTvDeviceId = (TextView) findViewById(R.id.tv_attendance_devices_id);
        mTvDeviceStatus = (TextView) findViewById(R.id.tv_attendance_devices_state);
        mTvCurDeviceName = (TextView) findViewById(R.id.tv_attendance_current_devices_name);
        mTvCurDeviceId = (TextView) findViewById(R.id.tv_attendance_current_devices_id);
        mTvCurDeviceBind = (TextView) findViewById(R.id.tv_attendance_current_devices_bind);
        mTvHistoryText = (TextView) findViewById(R.id.tv_attendance_devices_history);
        mLvHistoryDeviceList = (MyListView) findViewById(R.id.lv_attendance_devices_list);

        mTvCurDeviceBind.setOnClickListener(this);
        mLlBack.setOnClickListener(this);
    }


    private void getIntentData() {
        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("groupId");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_attendance_current_devices_bind:
                if (isOpen == 1) {
                   setMyNewDeviceMsg();
                }
                break;
            case R.id.ll_attendance_devices_back:
                finish();
                break;
        }
    }

    private void getCurDeviceMsg() {
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        mDeviceId = telephonyManager.getDeviceId();
        Build bd = new Build();
        Log.i("info", bd.MODEL + ":" + bd.DEVICE + ":" + bd.PRODUCT);
        mDeviceName=bd.MODEL;
        curDevice.setDeviceId(mDeviceId);
        curDevice.setDeviceName(mDeviceName);

        mTvCurDeviceId.setText("设备号:" + mDeviceId);
        mTvCurDeviceName.setText(mDeviceName);

        mTvDeviceName.setText(mDeviceName);
        mTvDeviceId.setText("设备号:" + mDeviceId);

    }

    /**
     * 我的设备
     */
    private void getMyDeviceMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        MyDeviceMsg myDeviceMsg = new MyDeviceMsg(mHandler.obtainMessage(GET_ATTENDANCE_GET_MY_DEVICE));
        myDeviceMsg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_MY_DEVICES;
        myDeviceMsg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(myDeviceMsg);
    }

    /**
     *   插入新设备并设置成我的默认设备
     */
    public void setMyNewDeviceMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_DEVICE_NUMBER, mDeviceId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_DEVICE_TYPE, "1"));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_DEVICE_NAME, mDeviceName));
        GetMsgHttpReceiver getMsgHttpReceiver = new GetMsgHttpReceiver(mHandler.obtainMessage(GET_ATTENDANCE_SET_MY_DEVICE));
        getMsgHttpReceiver.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_INSERT_NEW_DEFAULT_DEVICES;
        getMsgHttpReceiver.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(getMsgHttpReceiver);
    }

    /**
     * 设置我的默认设备
     */
    public void setMyDeviceMsg(String id) {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_DEVICE_ID, id));
        GetMsgHttpReceiver getMsgHttpReceiver = new GetMsgHttpReceiver(mHandler.obtainMessage(GET_ATTENDANCE_SET_MY_DEVICE));
        getMsgHttpReceiver.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_SET_DEFAULT_DEVICES;
        getMsgHttpReceiver.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(getMsgHttpReceiver);
    }
}
